function drawPalette(fg)
{
    var clr = new Array('00', '11', '22', '33', '44', '55', '66', '77', '88', '99', 'AA', 'BB', 'CC', 'DD', 'EE', 'FF');
    var min = 0;
    var max = clr.length - 1;
    var images = new Array();
    var d;

    function lightCase(e)
    {
        var c, dst;

        skyblog_recupcolor(this, !isFg(this));
        dst = document.getElementById(TAusedcolor + (isFg(this) ? '' : 'BG'));
        dst.style.backgroundColor = this.style.backgroundColor;

        if (!e)
            window.event.returnValue = false;
        else if (e.preventDefault)
            e.preventDefault();
    }

    function lightCase2(e)
    {
        var c, dst;

        dst = document.getElementById(TAactualcolor + (isFg(this) ? '' : 'BG'));
        dst.style.backgroundColor = this.style.backgroundColor;

        if (!e)
            window.event.returnValue = false;
        else if (e.preventDefault)
            e.preventDefault();
    }

    function isFg(elm)
    {
        elm = elm.parentNode;

        if (elm.id.indexOf('palettefg') == -1)
            return false;
        return true;
    }

    function addCell(r, g, b)
    {
        var a;
        var hex = '#' + r + g + b;

        a = document.createElement('a');
        a.style.backgroundColor = hex
        a.className   = 'btcolordyn';
        a.href        = '#';

        a.onclick     = lightCase;
        a.onmouseover = lightCase2;

        return a;
    }

    function doLoop(d, dir, loop, r, g, b)
    {
        var rgb = new Array(r, g, b);

        if (dir > 0)
        {
            for (rgb[loop] = 0; rgb[loop] < clr.length; rgb[loop]++)
                d.appendChild(addCell(clr[rgb[0]], clr[rgb[1]], clr[rgb[2]]));
        }
        else if (dir < 0)
        {
            for (rgb[loop] = clr.length - 1; rgb[loop] >= 0; rgb[loop]--)
                d.appendChild(addCell(clr[rgb[0]], clr[rgb[1]], clr[rgb[2]]));
        }
        else
        {
            for (rgb[0] = r; rgb[0] >= 0; rgb[0]--, rgb[1]--, rgb[2]--)
                d.appendChild(addCell(clr[rgb[0]], clr[rgb[1]], clr[rgb[2]]));
        }
    }

    if (document.getElementById)
    {
        if (fg)
            d = document.getElementById('palettefg');
        else
            d = document.getElementById('palette');
    }

    if (d)
    {
        /* couleur */
        doLoop(d,  1, 1, max, min, min);
        doLoop(d, -1, 0, max, max, min);
        doLoop(d,  1, 2, min, max, min);
        doLoop(d, -1, 1, min, max, max);
        doLoop(d,  1, 0, min, min, max);
        doLoop(d, -1, 2, max, min, max);

        /* noir et blanc */
        doLoop(d,  0, 2, max, max, max);
    }
}

var palette_popup = false;

function TAinsert(text1, text2)
{
    function countInstances(ta, open, closed)
    {
        var opening = ta.value.split(open);
        var closing = ta.value.split(closed);
        return opening.length + closing.length - 2;
    }

    var ta = document.getElementById(TAtextbox);

    if (document.selection)
    {
        ta.focus();
        var sel = document.selection.createRange();
        var str = sel.text;
    /*    var ran = sel.duplicate();
        var hack;*/

        if (text2 != "")
        {
            if (str == "")
            {
                var instances = countInstances(ta, text1, text2);
                if (instances % 2 != 0)
                {
                    str = sel.text + text2;
                }
                else
                {
                    str = sel.text + text1;
                }
            }
            else
            {
                str = text1 + sel.text + text2;
            }
        }
        else
        {
            str = sel.text + text1;
        }
        sel.text = str;
    /*    hack = ta.value.replace(/\r/g, '');
        ran.moveStart("character", 0);
        ran.moveEnd("character",   str.length - hack.length);
        ran.select();
        ta.focus();*/
    }
    else if (ta.selectionStart | ta.selectionStart == 0)
    {
        if (ta.selectionEnd > ta.value.length) { ta.selectionEnd = ta.value.length; }

        var firstPos = ta.selectionStart;
        var secondPos = ta.selectionEnd+text1.length;

        ta.value=ta.value.slice(0,firstPos)+text1+ta.value.slice(firstPos);
        ta.value=ta.value.slice(0,secondPos)+text2+ta.value.slice(secondPos);

        ta.selectionStart = firstPos+text1.length;
        ta.selectionEnd = secondPos;
        ta.focus();
    }
    else
    { // Opera
        var sel = ta; // document.post.message;

        var instances = countInstances(ta,text1,text2);
        if (instances%2 != 0 && text2 != ""){ sel.value = sel.value + text2; }
        else{ sel.value = sel.value + text1; }
    }
    return false;
}


function skyblog_recupcolor(obj,bg)
{
    // From HTMLArea
    function convercolor(v)
    {
        // returns the hex representation of one byte (2 digits)
        function hex(d) {
            return (d < 16) ? ("0" + d.toString(16)) : d.toString(16);
        };

        if (typeof v == "number") {
            // we're talking to IE here
            var r = v & 0xFF;
            var g = (v >> 8) & 0xFF;
            var b = (v >> 16) & 0xFF;
            return "#" + hex(r) + hex(g) + hex(b);
        }

        if (v.substr(0, 3) == "rgb") {
            // in rgb(...) form -- Mozilla and Opera
            var re = /rgb\s*\(\s*([0-9]+)\s*,\s*([0-9]+)\s*,\s*([0-9]+)\s*\)/;
            if (v.match(re)) {
                var r = parseInt(RegExp.$1);
                var g = parseInt(RegExp.$2);
                var b = parseInt(RegExp.$3);
                return "#" + hex(r) + hex(g) + hex(b);
            }
            // doesn't match RE?! maybe uses percentages or float numbers
            // -- FIXME--: not yet implemented.
            return null;
        }

        if (v.substr(0, 1) == "#") {
            // already hex rgb (hopefully :D )
            return v;
        }

        // if everything else fails ;)
        return null;
    }

    var tag;
    if (bg) { tag = "f"; } else { tag = "c"; }

    if (typeof obj.style.backgroundColor != 'undefined' && obj.style.backgroundColor.length > 0)
    {
        var color = convercolor(obj.style.backgroundColor);
        if (color != null)
            TAinsert('['+tag+'='+color+']','[/'+tag+']');
    }
}

function drawAdvancedPalette(e)
{
    var args = '';
    var height = 400;

    if (this.id.indexOf('bg') != -1)
        args += '&bg=1';
    if (this.id.indexOf('gradient') != -1)
    {
        args += '&gradient=1';
        height += 135;
    }
    if (palette_popup && !palette_popup.closed)
        palette_popup.close();
    palette_popup = window.open('palette.php?popup=1' + args, 'couleur', 'resizable=yes,dependent=yes,menubar=no,personalbar=no,scrollbars=no,toolbar=no,width=560,height=' + height);
    palette_popup.focus();
    return false;
}

function getAdvPaletteValue(isbg, curcolor)
{
    var dst = document.getElementById(TAusedcolor + (isbg ? 'BG' : ''));

    dst.style.backgroundColor = curcolor;
    skyblog_recupcolor(dst, isbg);
    palette_popup.close();
}

function getGradientValues(isbg, values)
{
    var str = '';

    for (var i = 0; i < values.length; i++)
    {
        str += '-' + values[i];
    }
    str = str.substr(1);

    if (isbg)
        TAinsert('[y=' + str + ']','[/y]');
    else
        TAinsert('[x=' + str + ']','[/x]');
    hideAdvancedPalette(null);
    return false;
}

function showAdvancedPalette()
{
	palette_popup.focus();
}

function hideAdvancedPalette()
{
	palette_popup.close();
}

function revertTemplateSelection(id)
{
    var elm;

    selectTemplate(id, false);
    try
    {
        elm = document.getElementById('template_alert');
        elm.style.display = 'none';

        elm = document.getElementById('template_infos_selected');
        elm.style.display = 'none';
    }
    catch (e)
    {
        // console.log(e);
    }
}

function selectTemplatePopup(id, tplName)
{
    if (self.opener && self.opener.document)
    {
        var s = self.opener.document;
        var elm = s.getElementById('id_template');
        
        old_template_id = elm.value;    
        elm.value = id;
        
        elm = s.getElementById('img_template');
        elm.src = elm.src.replace(old_template_id, id);
        elm.style.borderColor = '#336699';
        
        elm = s.getElementById('txt_template');
        elm.innerHTML = tplName;
        
        old_template_id = id;
        
        self.close();
    }
    return;    
}


function selectTemplate(id, tplName)
{
    var elm;
    
    if (typeof mode_popup != 'undefined' && mode_popup)
    {
        return selectTemplatePopup(id, tplName);
    }

    try
    {
        elm = document.getElementById('h' + old_template_id);
        if (elm)
        {
            elm.parentNode.className = 'tpl_notselected';
        }

        elm = document.getElementById('h' + id);
        elm.parentNode.className = 'tpl_selected';

        elm = document.getElementById('template_alert');
        elm.style.display = 'block';

        elm = document.getElementById('template_infos_selected');
        elm.style.display = 'block';

        elm = document.getElementById('template_selected');
        elm.src = elm.src.replace(old_template_id, id);

        elm = document.getElementById('template_current_msg');

        if (tplName)
        {
            elm.innerHTML = tplName;
        }
        else if (id < 100)
        {
            tpl_classic_message = tpl_classic_message.replace(old_template_id, id);
            elm.innerHTML = tpl_classic_message;
        }
        else if (id < 200)
        {
            tpl_new_message = tpl_new_message.replace(old_template_id, id);
            elm.innerHTML = tpl_new_message;
        }
        elm = document.getElementById('id_template');
        elm.value = id;
        old_template_id = id;
    }
    catch (e)
    {
      //  console.log(e);
    }
}